package edu.thu.thss.yxy.util;

import java.io.InputStream;
import java.net.URL;
import java.util.Properties;

import edu.thu.thss.yxy.configuration.ConfigurationManager;

/**
 * Common Properties Tasks
 * 
 * @author Andy Gao
 * 
 */
public class PropertiesUtil {
	/**
	 * Returns a property from the sip-communicator properties set.
	 * 
	 * @param property
	 * @return
	 */
	public static String getProperty(String property) {
		return ConfigurationManager.getProperty(property);
	}

	public static String getSystemProperty(String property) {
		String retval = System.getProperty(property);
		if (retval == null) {

			return retval;
		}
		if (retval.trim().length() == 0) {
			return null;
		}
		return retval;
	}

	/**
	 * @param propertyName
	 * @param propertyValue
	 */
	public static void setProperty(String propertyName, String propertyValue) {
		ConfigurationManager.setProperty(propertyName, propertyValue);
	}

	public static void setSystemProperty(String propertyName,
			String propertyValue) {
		System.setProperty(propertyName, propertyValue);
	}

	public static URL getResource(String name) {
		return PropertiesUtil.class.getResource("/resources/" + name);
	}

	public static InputStream getResourceAsStream(String name) {
		return PropertiesUtil.class.getResourceAsStream("/resources/" + name);
	}

	public static Properties getCurrProperties() {
		return ConfigurationManager.getProperties();
	}

	public static Properties getSipProperties() {
		Properties properties = new Properties();
		String stackName = ConfigurationManager.getProperty("javax.sip.STACK_NAME");
		if(stackName != null && !"".equals(stackName)){
			properties.put("javax.sip.STACK_NAME", stackName);
		}
		
		String ipAddress = ConfigurationManager.getProperty("javax.sip.IP_ADDRESS");
		if(ipAddress != null && !"".equals(ipAddress)){
			properties.put("javax.sip.IP_ADDRESS", ipAddress);
		}
		
		String routerPath = ConfigurationManager.getProperty("javax.sip.ROUTER_PATH");
		if(routerPath != null && !"".equals(routerPath)){
			properties.put("javax.sip.ROUTER_PATH", routerPath);
		}
		
		String reTransFilter = ConfigurationManager.getProperty("javax.sip.RETRANSMISSON_FILTER");
		if(reTransFilter != null && !"".equals(reTransFilter)){
			properties.put("javax.sip.RETRANSMISSON_FILTER", reTransFilter);
		}
		
		String outboundProxy = ConfigurationManager.getProperty("javax.sip.OUTBOUND_PROXY");
		if(outboundProxy != null && !"".equals(outboundProxy)){
			properties.put("javax.sip.OUTBOUND_PROXY", stackName);
		}
		
		String extensionMethods = ConfigurationManager.getProperty("javax.sip.EXTENSION_METHODS");
		if(extensionMethods != null && !"".equals(extensionMethods)){
			properties.put("javax.sip.EXTENSION_METHODS", extensionMethods);
		}
		
		String serverLog = ConfigurationManager.getProperty("gov.nist.javax.sip.SERVER_LOG");
		if(serverLog != null && !"".equals(serverLog)){
			properties.put("gov.nist.javax.sip.SERVER_LOG", serverLog);
		}
		
		String traceLevel = ConfigurationManager.getProperty("gov.nist.javax.sip.TRACE_LEVEL");
		if(traceLevel != null && !"".equals(traceLevel)){
			properties.put("gov.nist.javax.sip.TRACE_LEVEL", traceLevel);
		}

		return properties;
	}
}
